# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.


from odoo import http, api
from odoo.http import request
from odoo import registry as registry_get
from odoo.service import security

from odoo import SUPERUSER_ID, _



import logging
_logger = logging.getLogger(__name__)


class Iot(http.Controller):
    @http.route('/api/iot/gateway/register',type='json', auth='none', cors='*',csrf=False)
    def gateway_register(self, db, code):
        print('hello', db, code)
        registry = registry_get(db)
        context = {}
        with registry.cursor() as cr:
            env = api.Environment(cr, SUPERUSER_ID, context)
            Model = env['iot.gateway']

            res = Model.search([('code','=',code)])

            if res:
                return res

            ret = Model.create({
                'code': code,
                'name': code,
                'user_id': None,
                'company_id': None,
            })

            return ret

